package com.jiangzehui.www;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * Polygonsview
 */
public class Polygonsview extends Component implements Component.DrawTask {
    private int center; // 中心点
    private float oneRadius; // 外层菱形圆半径
    private float distance; // 多边形之间的间距
    private int defalutSize = 300; // 默认大小
    private String[] str = {"击杀", "生存", "助攻", "物理", "魔法", "防御", "金钱"};
    private Rect strRect; // 字体矩形
    private Paint rankPaint; // 各等级进度画笔
    private Paint strPaint; // 字体画笔
    private Paint centerPaint; // 中心线画笔
    private Paint onePaint; // 最外层多边形画笔
    private Paint twoPaint; // 第二层多边形画笔
    private Paint threePaint; // 第三层多边形画笔
    private Paint fourPaint; // 第四层多边形画笔
    private float f1;
    private float f2;
    private float f3;
    private float f4;
    private float f5;
    private float f6;
    private float f7;
    private boolean isWrapContent;
    private boolean init;

    public Polygonsview(Context context) {
        this(context, null);
    }

    public Polygonsview(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    public Polygonsview(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        defalutSize = dp2px(defalutSize);
        // 初始化字体画笔
        strPaint = new Paint();
        strPaint.setAntiAlias(true);
        strPaint.setColor(Color.BLACK);
        strPaint.setTextSize(dp2px(16));
        strRect = new Rect();
        strRect = strPaint.getTextBounds(str[0]);

        // 初始化各等级进度画笔
        rankPaint = new Paint();
        rankPaint.setAntiAlias(true);
        rankPaint.setColor(Color.RED);
        rankPaint.setStrokeWidth(8);
        rankPaint.setStyle(Paint.Style.STROKE_STYLE); // 设置空心

        // 初始化最外层多边形画笔
        onePaint = new Paint();
        onePaint.setAntiAlias(true);
        ohos.global.resource.ResourceManager resManager = getContext().getResourceManager();
        try {
            int color = resManager.getElement(ResourceTable.Color_one).getColor();
            onePaint.setColor(new Color(color));
        } catch (IOException e) {
            LogUtil.info("IOException","IOException");
        } catch (NotExistException e) {
            LogUtil.info("NotExistException","NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.info("WrongTypeException","WrongTypeException");
        }

        onePaint.setStyle(Paint.Style.FILL_STYLE); // 设置实心

        // 初始化第二层多边形画笔
        twoPaint = new Paint();
        twoPaint.setAntiAlias(true);
        try {
            int colorTwo = resManager.getElement(ResourceTable.Color_two).getColor();
            twoPaint.setColor(new Color(colorTwo));
        } catch (IOException e) {
            LogUtil.info("IOException","IOException");
        } catch (NotExistException e) {
            LogUtil.info("NotExistException","NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.info("WrongTypeException","WrongTypeException");
        }

        twoPaint.setStyle(Paint.Style.FILL_STYLE); // 设置实心

        // 初始化第三层多边形画笔
        threePaint = new Paint();
        threePaint.setAntiAlias(true);
        int colorThree = 0;
        try {
            colorThree = resManager.getElement(ResourceTable.Color_three).getColor();
        } catch (IOException e) {
            LogUtil.info("IOException","IOException");
        } catch (NotExistException e) {
            LogUtil.info("NotExistException","NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.info("WrongTypeException","WrongTypeException");
        }
        threePaint.setColor(new Color(colorThree));
        threePaint.setStyle(Paint.Style.FILL_STYLE); // 设置实心

        // 初始化最内层多边形画笔
        fourPaint = new Paint();
        fourPaint.setAntiAlias(true);
        int colorFour = 0;
        try {
            colorFour = resManager.getElement(ResourceTable.Color_four).getColor();
        } catch (IOException e) {
            LogUtil.info("IOException","IOException");
        } catch (NotExistException e) {
            LogUtil.info("NotExistException","NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.info("WrongTypeException","WrongTypeException");
        }
        fourPaint.setColor(new Color(colorFour));
        fourPaint.setStyle(Paint.Style.FILL_STYLE); // 设置实心

        // 初始化中心线画笔
        centerPaint = new Paint();
        centerPaint.setAntiAlias(true);
        centerPaint.setColor(Color.WHITE);

        addDrawTask(this);
        

    }


    /**
     * Sets str *
     *
     * @param str str
     */
    public void setStr(String[] str) {
        if (str != null && str.length == 7) {
            this.str = str;
            invalidate();
        }
    }

    /**
     * Sets one color *
     *
     * @param color color
     */
    public void setOneColor(int color) {
        onePaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * Sets two color *
     *
     * @param color color
     */
    public void setTwoColor(int color) {
        twoPaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * Sets three color *
     *
     * @param color color
     */
    public void setThreeColor(int color) {
        threePaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * Sets four color *
     *
     * @param color color
     */
    public void setFourColor(int color) {
        fourPaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * Sets center color *
     *
     * @param color color
     */
    public void setCenterColor(int color) {
        centerPaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * Sets str color *
     *
     * @param color color
     */
    public void setStrColor(int color) {
        strPaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * Sets rank color *
     *
     * @param color color
     */
    public void setRankColor(int color) {
        rankPaint.setColor(new Color(color));
        invalidate();
    }


    /**
     * Sets value 1 *
     *
     * @param value value
     */
    public void setValue1(float value) {
        f1 = oneRadius - oneRadius / 4 * value;
        addDrawTask(this);
    }

    /**
     * Sets value 2 *
     *
     * @param value value
     */
    public void setValue2(float value) {
        f2 = oneRadius - oneRadius / 4 * value;
        invalidate();
    }

    /**
     * Sets value 3 *
     *
     * @param value value
     */
    public void setValue3(float value) {
        f3 = oneRadius - oneRadius / 4 * value;
        invalidate();
    }

    /**
     * Sets value 4 *
     *
     * @param value value
     */
    public void setValue4(float value) {
        f4 = oneRadius - oneRadius / 4 * value;
        invalidate();
    }

    /**
     * Sets value 5 *
     *
     * @param value value
     */
    public void setValue5(float value) {
        f5 = oneRadius - oneRadius / 4 * value;
        invalidate();
    }

    /**
     * Sets value 6 *
     *
     * @param value value
     */
    public void setValue6(float value) {
        f6 = oneRadius - oneRadius / 4 * value;
        invalidate();
    }

    /**
     * Sets value 7 *
     *
     * @param value value
     */
    public void setValue7(float value) {
        f7 = oneRadius - oneRadius / 4 * value;
        invalidate();
    }


    /**
     * Paint rank *
     *
     * @param canvas canvas
     */
    private void paintRank(Canvas canvas) {

        Path path = new Path();
        path.moveTo(center, getPaddingTop() + 2 * strRect.getHeight() + f1);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7)) * (oneRadius - f2)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - f2))));
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - f3)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - f3)) + center);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - f4)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - f4)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - f5)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - f5)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - f6)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - f6)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7)) * (oneRadius - f7)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - f7))));
        path.close();
        canvas.drawPath(path, rankPaint);

    }


    /**
     * Paint fout *
     *
     * @param canvas canvas
     */
    private void paintFout(Canvas canvas) {
        canvas.drawText(strPaint, str[0], center - strRect.getWidth() / 2, (float) (getPaddingTop() + 0.8 * strRect.getWidth()));
        canvas.drawText(strPaint, str[1], (float) (center + Math.sin(Math.toRadians(360 / 7)) * oneRadius + strRect.getHeight() / 2), (float) ((getPaddingTop() + 2 * strRect.getHeight() + oneRadius - Math.abs(Math.cos(Math.toRadians(360 / 7)) * oneRadius))));
        canvas.drawText(strPaint, str[2], (float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius + strRect.getHeight() / 2), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius) + center + strRect.getHeight() / 2);
        canvas.drawText(strPaint, str[3], (float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * oneRadius - strRect.getHeight() / 2 + strRect.getWidth() / 2), (float) ((Math.cos(Math.toRadians(360 / 7 / 2)) * oneRadius) + center + strRect.getHeight()));
        canvas.drawText(strPaint, str[4], (float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * oneRadius + strRect.getHeight() / 2 - strRect.getWidth() * 1.5), (float) ((Math.cos(Math.toRadians(360 / 7 / 2)) * oneRadius) + center + strRect.getHeight()));
        canvas.drawText(strPaint, str[5], (float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius - strRect.getHeight() / 2 - strRect.getWidth()), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius) + center + strRect.getHeight() / 2);
        canvas.drawText(strPaint, str[6], (float) (center - Math.sin(Math.toRadians(360 / 7)) * oneRadius - strRect.getHeight() / 2 - strRect.getWidth()), (float) ((getPaddingTop() + 2 * strRect.getHeight() + oneRadius - Math.abs(Math.cos(Math.toRadians(360 / 7)) * oneRadius))));

    }

    private void onePolygons(Canvas canvas) {
        Path path = new Path();
        path.moveTo(center, getPaddingTop() + 2 * strRect.getHeight()); // 起始点
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7)) * oneRadius), (float) (getPaddingTop() + 2 * strRect.getHeight() + oneRadius - Math.abs(Math.cos(Math.toRadians(360 / 7)) * oneRadius)));
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius) + center);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * oneRadius), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * oneRadius) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * oneRadius), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * oneRadius) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * oneRadius) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7)) * oneRadius), (float) (getPaddingTop() + 2 * strRect.getHeight() + oneRadius - Math.abs(Math.cos(Math.toRadians(360 / 7)) * oneRadius)));
        path.close();
        canvas.drawPath(path, onePaint);
    }

    private void twoPolygons(Canvas canvas) {
        distance = oneRadius / 4;
        Path path = new Path();
        path.moveTo(center, getPaddingTop() + 2 * strRect.getHeight() + distance);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7)) * (oneRadius - distance)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - distance))));
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7)) * (oneRadius - distance)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - distance))));
        path.close();
        canvas.drawPath(path, twoPaint);
    }

    private void threePolygons(Canvas canvas) {
        distance = oneRadius / 2;
        Path path = new Path();
        path.moveTo(center, getPaddingTop() + 2 * strRect.getHeight() + distance);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7)) * (oneRadius - distance)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - distance))));
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7)) * (oneRadius - distance)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - distance))));

        path.close();
        canvas.drawPath(path, threePaint);
    }

    private void fourPolygons(Canvas canvas) {
        distance = oneRadius / 2 + oneRadius / 4;
        Path path = new Path();
        path.moveTo(center, getPaddingTop() + 2 * strRect.getHeight() + distance);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7)) * (oneRadius - distance)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - distance))));
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center + Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)), (float) (Math.cos(Math.toRadians(360 / 7 + 360 / 7 / 2)) * (oneRadius - distance)) + center);
        path.lineTo((float) (center - Math.sin(Math.toRadians(360 / 7)) * (oneRadius - distance)), (float) (getPaddingTop() + 2 * strRect.getHeight() + (oneRadius) - Math.abs(Math.cos(Math.toRadians(360 / 7)) * (oneRadius - distance))));

        path.close();
        canvas.drawPath(path, fourPaint);
    }

    private void center(Canvas canvas) {
        // 绘制七边形中心线
        canvas.save(); // 保存当前状态
        canvas.rotate(0, center, center);
        float startY = getPaddingTop() + 2 * strRect.getHeight();
        float endY = center;
        float du = (float) (360 / 7 + 0.5);
        for (int i = 0; i < 7; i++) {
            Point startPoint = new Point(center, startY);
            Point endPoint = new Point(center, endY);
            canvas.drawLine(startPoint, endPoint, centerPaint);
            canvas.rotate(du, center, center);

        }
        canvas.restore(); // 恢复之前状态
    }


    /**
     * Dp px int
     *
     * @param values values
     * @return the int
     */
    public int dp2px(int values) {
        return (int) (getContext().getResourceManager().getDeviceCapability().screenDensity / 160 * values);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        
        if(!init){
            center = getWidth() / 2; // 中心点
            oneRadius = center - getPaddingTop() - 2 * strRect.getHeight();
            f1 = oneRadius - oneRadius / 4 * 1;
            f2 = oneRadius - oneRadius / 4 * 1;
            f3 = oneRadius - oneRadius / 4 * 1;
            f4 = oneRadius - oneRadius / 4 * 1;
            f5 = oneRadius - oneRadius / 4 * 1;
            f6 = oneRadius - oneRadius / 4 * 1;
            f7 = oneRadius - oneRadius / 4 * 1;
            init = true;
        }
        paintFout(canvas);
        onePolygons(canvas);
        twoPolygons(canvas);
        threePolygons(canvas);
        fourPolygons(canvas);
        center(canvas);
        paintRank(canvas);
    }
}

